<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>影片管理</el-breadcrumb-item>
      <el-breadcrumb-item>电影列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <!-- 搜索与添加区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="query"
            clearable
            @clear="getMovieList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getMovieByName"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加电影</el-button
          >
        </el-col>
      </el-row>
      <el-table :data="movielits" stripe border>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="电影图片" prop="posters" align="center">
          <template slot-scope="scope">
            <img class="img" :src="scope.row.posters" alt="" srcset="" />
          </template>
        </el-table-column>
        <el-table-column
          label="电影名字"
          prop="name"
          align="center"
          width="150px"
        ></el-table-column>
        <el-table-column label="电影简介" prop="Introduction" align="center">
          <template slot-scope="scope">
            <p class="showOverTooltip">{{ scope.row.Introduction }}</p>
          </template>
        </el-table-column>
        <el-table-column
          label="类型"
          prop="category"
          align="center"
          width="150px"
        >
          <template slot-scope="scope">
            <el-tag>{{ scope.row.category }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="电影价格(元)"
          prop="price"
          align="center"
          width="150px"
        ></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeMovieById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加电影 -->
    <el-dialog
      title="添加电影"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClosed"
    >
      <!-- 内容主体区域 -->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="电影名" prop="name" label-width="100px">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="category" label-width="100px">
          <el-input v-model="addForm.category"></el-input>
        </el-form-item>
        <el-form-item label="导演" prop="director" label-width="100px">
          <el-input v-model="addForm.director"></el-input>
        </el-form-item>
        <el-form-item label="主演" prop="starring" label-width="100px">
          <el-input v-model="addForm.starring"></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="price" label-width="100px">
          <el-input v-model="addForm.price"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="Introduction" label-width="100px">
          <el-input
            v-model="addForm.Introduction"
            type="textarea"
          ></el-input>
        </el-form-item>
        <el-form-item label="海报" prop="posters" label-width="100px">
          <el-upload
            :limit="1"
            :action="uploadURL"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            list-type="picture"
            :headers="headersObj"
            :on-success="handleSuccess"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addMovie">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改电影 -->
    <el-dialog
      title="修改电影信息"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <!-- 内容主体区域 -->
      <el-form
        :model="editForm"
        :rules="editFormRef"
        ref="editFormRef"
        label-width="70px"
      >
        <el-form-item label="电影名" prop="name" label-width="100px">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="category" label-width="100px">
          <el-input v-model="editForm.category"></el-input>
        </el-form-item>
        <el-form-item label="导演" prop="director" label-width="100px">
          <el-input v-model="editForm.director"></el-input>
        </el-form-item>
        <el-form-item label="主演" prop="starring" label-width="100px">
          <el-input v-model="editForm.starring"></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="price" label-width="100px">
          <el-input v-model="editForm.price"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="Introduction" label-width="100px">
          <el-input
            v-model="editForm.Introduction"
            type="textarea"
          ></el-input>
        </el-form-item>
        <el-form-item label="海报" prop="posters" label-width="100px">
          <el-upload
            :limit="1"
            :action="uploadURL"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            list-type="picture"
            :headers="headersObj"
            :on-success="handleSuccess"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateMovie">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      query: "",
      movielits: [],
      //控制添加用户对话框的显示与隐藏
      addDialogVisible: false,
      //添加用户表单数据
      addForm: {
        name: "",
        category: "",
        director: "",
        starring: "",
        price: 0,
        Introduction: "",
        posters:""
      },
      //添加表单的验证规则对象
      addFormRules: {
        name: [{ required: true, message: "请输入电影名字", trigger: "blur" }],
        category: [
          { required: true, message: "请输入电影类型", trigger: "blur" },
        ],
        director: [{ required: true, message: "请输入导演", trigger: "blur" }],
        starring: [{ required: true, message: "请输入主演", trigger: "blur" }],
        price: [{ required: true, message: "请输入价格", trigger: "blur" }],
        Introduction:[{ required: true, message: "请输入简介", trigger: "blur" }]
      },
      editFormRef:{
        name: [{ required: true, message: "请输入电影名字", trigger: "blur" }],
        category: [
          { required: true, message: "请输入电影类型", trigger: "blur" },
        ],
        director: [{ required: true, message: "请输入导演", trigger: "blur" }],
        starring: [{ required: true, message: "请输入主演", trigger: "blur" }],
        price: [{ required: true, message: "请输入价格", trigger: "blur" }],
        Introduction:[{ required: true, message: "请输入简介", trigger: "blur" }]
      },
      uploadURL: "http://127.0.0.1:3007/uploadImg",
      headersObj: {
        Authorization: window.sessionStorage.getItem("token"),
      },
      editDialogVisible:false,
      editForm:{}
    };
  },
  created() {
    this.getMovieList();
  },
  methods: {
    async getMovieList() {
      const { data: res } = await this.$http.get("movieAll");
      if (res.status !== 200) return this.$message.error("获取失败");
      this.$message.success("获取成功");
      this.movielits = res.data;
    },
    async getMovieByName() {
      const { data: res } = await this.$http.get("search?name=" + this.query);
      if (res.status !== 200) return this.$message.error("获取失败");
      this.movielits = res.data;
    },

    //处理图片预览效果
    handlePreview(file) {
      this.previewPath = file.response.data.url;
      this.previewVisible = true;
    },
    //处理移除图片的操作
    handleRemove(file) {
      this.addForm.posters = "";
    },
    //监听图片上传成功的事件
    handleSuccess(response) {
      this.addForm.posters = response.data.img; 
    },
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    addMovie() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) {
          return this.$message.error("请填写必要的表单项！");
        }
        const {data:res} = await this.$http.post('movieAdd',{movieinfo:this.addForm})
        console.log(res);
        if(res.status !== 200){
          return this.$message.error('添加电影失败！')
        }

        this.$message.success('添加电影成功！')
        this.addDialogVisible = false
        this.getMovieList()
      });
    },
    editDialogClosed(){
        this.editDialogVisible = false
    },
     //展示编辑用户的对话框
    async showEditDialog(id) {
      const { data: res } = await this.$http.get('movieinfo/' + id)

      if (res.status != 200) {
        this.$message.error('获取电影失败！')
      }
      console.log(res);
      this.editForm = res.data.movieinfo

      this.editDialogVisible = true
    },
    updateMovie(){
         this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return
        //发起修改用户信息的数据请求
        const { data: res } = await this.$http.put(
          'movieinfo',
          {id:this.editForm.id, name: this.editForm.name, category: this.editForm.category,director:this.editForm.director,starring:this.editForm.starring,price:this.editForm.price,Introduction:this.editForm.Introduction,posters:this.editForm.posters }
        )
        if (res.status !== 200) {
          return this.$message.error('更新电影信息失败！')
        }

        //关闭对话框
        this.editDialogVisible = false
        //刷新列表
        this.getMovieList()
        //提示用户修改成功
        this.$message.success('更新电影信息成功！')
      })
      
    },
     //根据id删除对应的用户信息
    async removeMovieById(id) {
      //弹框询问用户是否删除数据
      const confirmResult = await this.$confirm(
        '此操作将永久删除该用户, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)

      //如果用户确认删除，则返回值为字符串 confirm
      //如果用户取消了删除，则返回值为字符串 cancel
      // console.log(confirmResult);
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }

      const { data: res } = await this.$http.delete('movieinfo/' + id)
      if (res.status !== 200) {
        return this.$message.error('删除电影失败！')
      }

      this.$message.success('删除电影成功！')
      this.getMovieList()
    }
  },
};
</script>

<style lang="less" scoped>
.img {
  width: 100px;
  height: 150px;
  margin: 0 auto;
}
.showOverTooltip {
  display: -webkit-box;
  text-overflow: ellipsis;
  overflow: hidden;
  /*这里是3行*/
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}
</style>